package janelas;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import outros.GeradorRelatorio;
import persistencia.DAOArea;
import persistencia.DAOGenerico;
import persistencia.DAOSetor;
import transporte.TOArea;
import transporte.TOGenerico;
import transporte.TOSetor;

public class TermoResponsabilidade extends javax.swing.JInternalFrame implements JanelaPai {
    private ArrayList<TOGenerico> orgao = new ArrayList();
    private ArrayList<TOArea> area = new ArrayList();
    private ArrayList<TOSetor> setor = new ArrayList();
    private DAOGenerico daoGenerico = new DAOGenerico();
    private DAOArea daoArea = new DAOArea();
    private DAOSetor daoSetor = new DAOSetor();
    private GeradorRelatorio gr = new GeradorRelatorio();

    public TermoResponsabilidade() {
        initComponents();
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-getWidth())/2, (int)((screenSize.height-getHeight())/3), getWidth(), getHeight());
        reiniciar();
    }

    public void reiniciar() {
        orgao = daoGenerico.selecionarGenerico("orgao", "nome");
        area = daoArea.selecionar("");
        setor = daoSetor.selecionar("");
        preencherComboBox();
    }

    private void preencherComboBox() {
        camOrgao.removeAllItems();
        camArea.removeAllItems();
        camSetor.removeAllItems();
        for (int i=0;i<orgao.size();i+=1) {
            camOrgao.addItem(orgao.get(i).getNome());
        }
        for (int i=0;i<area.size();i+=1) {
            camArea.addItem(area.get(i).getNome());
        }
        for (int i=0;i<setor.size();i+=1) {
            camSetor.addItem(setor.get(i).getNome());
        }
    }

    private String montaSQL() {
        Integer numeroSetor = setor.get(camSetor.getSelectedIndex()).getNumero();
        Integer numeroOrgao = orgao.get(camOrgao.getSelectedIndex()).getNumero();
        Integer numeroArea = area.get(camArea.getSelectedIndex()).getNumero();
        String sql = "SELECT patrimonio.area as numeroArea, patrimonio.grupo as numeroGrupo, patrimonio.subgrupo as numeroSubgrupo, patrimonio.numero as numeroPatrimonio, situacao.nome as situacao, patrimonio.serie as serie, patrimonio.grupo as g, patrimonio.subgrupo as s, patrimonio.nome as descricao, grupo.nome as grupo, subgrupo.nome as subgrupo FROM patri_patrimonio as patrimonio "+
                "LEFT JOIN patri_grupo as grupo ON (patrimonio.grupo=grupo.numero) "+
                "LEFT JOIN patri_subgrupo as subgrupo ON (patrimonio.subgrupo=subgrupo.numero AND subgrupo.grupo=patrimonio.grupo) "+
                "LEFT JOIN patri_situacao as situacao ON (patrimonio.situacao=situacao.numero)"+
                "WHERE patrimonio.lotacao="+numeroSetor+" AND patrimonio.orgao="+numeroOrgao+" AND patrimonio.area="+numeroArea+
                " ORDER BY grupo.nome, subgrupo.nome, patrimonio.nome";
        return sql;
    }

    private HashMap<String, String> getParametros() {
        HashMap<String, String> map = new HashMap();
        map.put("orgao", new String(camOrgao.getSelectedItem()+" - "+daoGenerico.selecionarCampo("orgao", "extenso", orgao.get(camOrgao.getSelectedIndex()).getNumero())).toUpperCase());
        Date data = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("'Goiânia, 'dd/MM/yyyy");
        String hoje = sdf.format(data);
        map.put("hoje", hoje);
        map.put("area", (String)camArea.getSelectedItem());
        map.put("setor", setor.get(camSetor.getSelectedIndex()).getNome());
        map.put("responsavel", setor.get(camSetor.getSelectedIndex()).getChefe());
        map.put("ramal", setor.get(camSetor.getSelectedIndex()).getRamal());
        return map;
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        camOrgao = new javax.swing.JComboBox();
        camArea = new javax.swing.JComboBox();
        camSetor = new javax.swing.JComboBox();
        botGerar = new javax.swing.JButton();

        setClosable(true);
        setTitle("Termo de Responsabilidade");

        jLabel1.setFont(new java.awt.Font("Segoe UI", 0, 13));
        jLabel1.setText("Órgão");

        jLabel2.setFont(new java.awt.Font("Segoe UI", 0, 13));
        jLabel2.setText("Área");

        jLabel3.setFont(new java.awt.Font("Segoe UI", 0, 13));
        jLabel3.setText("Setor");

        camOrgao.setFont(new java.awt.Font("Segoe UI", 0, 13));

        camArea.setFont(new java.awt.Font("Segoe UI", 0, 13));

        camSetor.setFont(new java.awt.Font("Segoe UI", 0, 13));

        botGerar.setFont(new java.awt.Font("Segoe UI", 0, 13)); // NOI18N
        botGerar.setText("Gerar");
        botGerar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botGerarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(camOrgao, 0, 374, Short.MAX_VALUE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(camArea, 0, 374, Short.MAX_VALUE)
                    .addComponent(jLabel3)
                    .addComponent(camSetor, 0, 374, Short.MAX_VALUE)
                    .addComponent(botGerar, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(camOrgao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(camArea, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(camSetor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(botGerar)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void botGerarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botGerarActionPerformed
        gr.gerarRelatorio(montaSQL(), "termo_responsabilidade", getParametros());
}//GEN-LAST:event_botGerarActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botGerar;
    private javax.swing.JComboBox camArea;
    private javax.swing.JComboBox camOrgao;
    private javax.swing.JComboBox camSetor;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    // End of variables declaration//GEN-END:variables
}
